//
//  GoodsTuWenViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/8/1.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "GoodsTuWenViewController.h"
#import <WebKit/WebKit.h>

@interface GoodsTuWenViewController ()<WKNavigationDelegate>

@end

@implementation GoodsTuWenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"图文详情"];
    
    NSString *str0 = [NSString stringWithFormat:@"%@%@%@",@"<!doctype html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"><style>body,div,p{font-size:14px;line-height:1.5em;}img {width:100%; height:auto;}</style></head><body>",self.tuWenStr,@"</body></html>"];
    NSString *str = [str0 stringByReplacingOccurrencesOfString:@"src=\"" withString:@"src=\"http:"];
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"src=\"http:http:" withString:@"src=\"http:"];
//    NSLog(@"---%@",str1);
    // 1.创建webview
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(10, SafeAreaTopHeight+10, kScreenWidth-20, kScreenHeight-SafeAreaTopHeight-10)];
    webView.navigationDelegate = self;
    
    [webView loadHTMLString:str1 baseURL:nil];
    // 最后将webView添加到界面
    [self.view addSubview:webView];
}
- (void)dealloc
{
    //    NSLog(@"控制器被销毁了");
    [self clearCache]; // 清除缓存
}
#pragma mark - wknavigationdelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    //    NSLog(@"0000000000000");
    [MBProgressHUD showMessage:@""];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    //    NSLog(@"sssssssssssss");
    [MBProgressHUD hideHUD];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    //    NSLog(@"ffffffffffff");
    [MBProgressHUD hideHUD];
}
- (void)clearCache
{
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 8) {
        NSArray * types =@[WKWebsiteDataTypeMemoryCache,WKWebsiteDataTypeDiskCache]; // 9.0之后才有的
        NSSet *websiteDataTypes = [NSSet setWithArray:types];
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        }];
        
    } else {
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
        //        NSLog(@"%@", cookiesFolderPath);
        NSError *errors;
        [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
